﻿/* Write a program that compares two text files line by line and prints the 
 * number of lines that are the same and the number of lines that are different. 
 * Assume the files have equal number of lines.
 * 
*/

using System;
using System.IO;

namespace _5.CompareFiles
{
    class CompareFiles
    {
        static void Main()
        {
            Console.Write("Path to first file (along with the filename): ");
            string path_to_file = Console.ReadLine();

            Console.Write("Path to second file (along with the filename): ");
            string path_to_file2 = Console.ReadLine();

            int countmatches = 0, countdifferences = 0;

            /* read files */
            StreamReader SR = new StreamReader(@path_to_file);
            string line = SR.ReadLine();

            try
            {
                using (SR)
                {
                    StreamReader SR2 = new StreamReader(@path_to_file2);
                    string line_ = SR2.ReadLine();
                    try
                    {
                        using (SR2)
                        {
                            while (line != null && line_ != null)
                            {
                                if (line == line_) countmatches++;
                                else countdifferences++;
                                line_ = SR2.ReadLine();
                                line = SR.ReadLine();
                            }
                        }
                    }
                    catch (FileNotFoundException f)
                    {
                        throw new FileNotFoundException("The specified file could not be found" + f.Message);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("The file could not be read:");
                        Console.WriteLine(e.Message);
                    }
                }
            }
            catch (FileNotFoundException f2)
            {
                throw new FileNotFoundException("The specified file could not be found" + f2.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("Lines that are same: {0} counted\r\nLines that are different: {1}", countmatches, countdifferences);
        }
    }
}
